package cn.edu.lit.jsjxy.CourseExample.chapter8;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import cn.edu.lit.jsjxy.CourseExample.R;
import cn.edu.lit.jsjxy.CourseExample.utils.MessageUtils;

public class FileOperationActivity extends Activity implements View.OnClickListener {
    Button btnWrite2File, btnWrite2SD, btnReadFromFile, btnReadFromSD;
    EditText etWrite2File, etWrite2SD;
    TextView tvReadFromFile, tvReadFromSD;

    String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_operation);

        btnReadFromFile = findViewById(R.id.btnReadFromFile);
        btnReadFromSD = findViewById(R.id.btnReadFromSDCard);
        btnWrite2File = findViewById(R.id.btnWriteToFile);
        btnWrite2SD = findViewById(R.id.btnWriteToSDCard);

        etWrite2File = findViewById(R.id.etWriteToFile);
        etWrite2SD = findViewById(R.id.etWriteToSDCard);
        tvReadFromFile = findViewById(R.id.tvReadFromFile);
        tvReadFromSD = findViewById(R.id.tvReadFromSDCard);

        btnReadFromFile.setOnClickListener(this);
        btnReadFromSD.setOnClickListener(this);
        btnWrite2File.setOnClickListener(this);
        btnWrite2SD.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnWriteToFile:
                write2File("InternalTest");
                break;
            case R.id.btnWriteToSDCard:
                write2SDCard("sdTest");
                break;
            case R.id.btnReadFromSDCard:
                readFromSDCard("sdTest");
                break;
            case R.id.btnReadFromFile:
                readFromFile("InternalTest");
                break;
        }
    }

    private void write2File(String filename) {
        OutputStream out;
        String strTowrite;
        try {
            out = openFileOutput(filename, MODE_PRIVATE|MODE_APPEND);
            strTowrite = etWrite2File.getText().toString();

            MessageUtils.showMsgByToast(this,strTowrite );

            byte[] bytes2Write = strTowrite.getBytes();

            out.write(bytes2Write);
            out.flush();
            out.close();

            MessageUtils.showMsgByToast(this, "已写入内部存储");

        } catch (IOException ex) {
            Log.e(TAG, ex.getMessage());
            Log.e(TAG, ex.getStackTrace().toString());
        }
    }

    private void write2SDCard(String fileName) {
        OutputStream out;
        String strTowrite;
        try {
            if (! Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ) {
                Toast.makeText(this, Environment.getExternalStorageState(), Toast.LENGTH_SHORT).show();

                Log.d(TAG, Environment.getExternalStorageState() + ":" + Environment.MEDIA_MOUNTED);
                MessageUtils.showMsgByToast(this, "找不到SD卡");
                return;
            }



            File SDRoot = Environment.getExternalStorageDirectory();
            File file2Write = new File(SDRoot, fileName);
            Log.d(TAG, file2Write.getAbsolutePath());

            out = new FileOutputStream(file2Write);

            //out = openFileOutput(path, MODE_PRIVATE);
            strTowrite = etWrite2SD.getText().toString();

            byte[] bytes2Write = strTowrite.getBytes();

            out.write(bytes2Write);
            out.flush();
            out.close();

            MessageUtils.showMsgByToast(this, "已写入SD卡");
        } catch (IOException ex) {
            Log.e(TAG, ex.getMessage());
            Log.e(TAG, ex.getSuppressed().toString());
        }
    }

    private void readFromFile(String filename) {
        InputStream in;
        try {
            in = openFileInput(filename);

            byte[] bytes = new byte[1024];

            int len = in.read(bytes);

            MessageUtils.showMsgByToast(this, new String(bytes,0,len));

//            while (in.available() != 0) {
//                int len = in.read(bytes);
//                strBuff.append(bytes.toString(),0,len-1);
//            }
//            MessageUtils.showMsgByToast(this, strBuff.toString());

            in.close();

            tvReadFromFile.setText(new String(bytes,0,len));
            MessageUtils.showMsgByToast(this, "已读取内部存储");
        } catch (IOException ex) {
            Log.e(TAG, ex.getMessage());
            Log.e(TAG, ex.getSuppressed().toString());
        }
    }

    private void readFromSDCard(String filename) {
        InputStream in;
        String strTowrite;
        File SDRoot;
        try {
//            in = openFileInput(filename);
            if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ) {
                MessageUtils.showMsgByToast(this, "未找到SD卡");
                return;
            }

            SDRoot = Environment.getExternalStorageDirectory();

            File file = new File(SDRoot, filename);
            FileInputStream fin = new FileInputStream(file);


            StringBuffer strBuff = new StringBuffer();

            byte[] bytes = new byte[1024];

            while (fin.available() != 0) {
                int len = fin.read(bytes);
                strBuff.append(new String(bytes,0,len));
            }

            fin.close();
            tvReadFromSD.setText(strBuff);

            MessageUtils.showMsgByToast(this, "已读取SD卡");



        } catch (IOException ex) {
            Log.e(TAG, ex.getMessage());
            Log.e(TAG, ex.getSuppressed().toString());
        }
    }
}
